package org.msfj.admin.data.service.impl;

import org.msfj.admin.data.service.MetadataService;
import org.msfj.admin.model.Metadata;
import org.msfj.admin.vo.MetadataVo;
import org.msfj.admin.data.service.MetadataService;
import org.msfj.admin.model.Metadata;
import org.msfj.admin.vo.MetadataVo;
import org.msfj.admin.data.service.MetadataService;
import org.msfj.admin.vo.MetadataVo;
import org.springframework.stereotype.Service;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import org.msfj.admin.model.Metadata;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheConfig;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 系统元数据service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "metadata")
public class MetadataServiceImpl extends BaseServiceImpl<Metadata> implements MetadataService {

    /**
     * 根据条件分页查询系统元数据列表
     *
     * @param metadataVo
     * @param page 当前第几页
     * @param rows 每页多少条记录
     * @return PageInfo<Metadata>
     */
    @Override
    //@Cacheable
    public PageInfo<MetadataVo> getByMetadata(MetadataVo metadataVo, int page, int rows){
        Example example = new Example(Metadata.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        PageHelper.startPage(page, rows);
        List<Metadata> metadataList = super.getByExample(example);
        PageInfo metadataVoPageInfo = new PageInfo(metadataList);
        metadataVoPageInfo.setList(setOtherProperties(metadataList));
        return metadataVoPageInfo;
    }

    /**
     * 根据条件查询所有系统元数据对象列表
     *
     * @param metadataVo
     * @return List<Metadata>
     */
    @Override
    //@Cacheable
    public List<MetadataVo> getAllMetadata(MetadataVo metadataVo){
        Example example = new Example(Metadata.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        List<MetadataVo> metadataVoList = setOtherProperties(super.getByExample(example));
        return metadataVoList;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param metadata
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public MetadataVo setOtherProperties(Metadata metadata){
        MetadataVo metadataVo = new MetadataVo();
        BeanUtils.copyProperties(metadata,metadataVo);
        return metadataVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param metadatas
     * @return List<MetadataVo>
    */
    @Override
    //@Cacheable
    public List<MetadataVo> setOtherProperties(List<Metadata> metadatas){
        List<MetadataVo> metadataVos = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(metadatas)){
            for(Metadata metadata : metadatas){
                MetadataVo metadataVo = new MetadataVo();
                BeanUtils.copyProperties(metadata,metadataVo);
                metadataVos.add(metadataVo);
            }
        }
        return metadataVos;
    }
}
